// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init({
  env: 'shop-9gu45y1p3f9a541f'
})

// 云函数入口函数
exports.main = async (event, context) => {
  const res = await cloud.cloudPay.unifiedOrder({
    "body": "琪琪的手工饰品屋-物品购买",
    "outTradeNo": event._id, //商品订单编号
    "spbillCreateIp": "127.0.0.1",
    "subMchId": "1637180636",
    "totalFee": parseInt(event.totalFee),
    "functionName": "payBack",
    "openid": event.openid,
    "tradeType": "JSAPI",
    "nonceStr": event.nonceStr, //32位随机字符串
    "envId": 'shop-9gu45y1p3f9a541f'
  })
  console.log(res)
  if (res.errCode === 0 && res.errMsg === "cloudPay.unifiedOrder:ok") {
    return res
    //return SendData(res.payment) //返回小程序端支付的参数
  } else {
    return [res.returnMsg,200,false]
    //return SendData(res.returnMsg, 200, false) //报个错
  }
}